package com.wjk.kylin.core.common.data;


import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpStatus;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

/**
 * @author wangjkui
 * @description 统一返回数据类型
 * @date 2020/5/22
 */
@Data
public class ResponseData implements Serializable{
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("成功标识")
    private Boolean success;

    @ApiModelProperty("返回码")
    private Integer code;

    @ApiModelProperty("返回信息")
    private String msg;

    @ApiModelProperty("返回数据")
    private Map<String,Object> data = MapUtil.newHashMap();

    private ResponseData() {
    }
    //===================error=====================================
    public static ResponseData error() {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, "未知异常，请联系管理员");
    }

    public static ResponseData error(String msg) {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, msg);
    }

    public static ResponseData error(Integer code, String msg) {
        ResponseData data = new ResponseData();
        data.setSuccess(false);
        data.setCode(code);
        data.setMsg(msg);
        return data;
    }


    //==============================success==========================
    public static ResponseData success() {
        return success("success");
    }

    public static ResponseData success(String msg) {
        return success(HttpStatus.HTTP_OK, msg);
    }

    public static ResponseData success(Integer code, String msg) {
        ResponseData data = new ResponseData();
        data.setCode(code);
        data.setSuccess(true);
        data.setMsg(msg);
        return data;
    }

    //==========================data==========================================

    public ResponseData data(String key,Object obj){
        this.data.put(key,obj);
        return this;
    }

    public ResponseData data(Map<String,Object> map){
        this.setData(map);
        return this;
    }
}
